class Solution:
    def largestNumber(self, nums: list) -> str:
        for i in range(len(nums)):
            nums[i] = str(nums[i])
        nums.sort(reverse=True)
        while True:
            flag = False
            for i in range(len(nums)-1):
                if int("".join([nums[i], nums[i+1]])) < int("".join([nums[i+1], nums[i]])):
                    nums[i], nums[i+1], flag = nums[i+1], nums[i], True
            if not flag:
                break
        # print(nums)
        if nums[0] == "0":
            return "0"
        return "".join(nums)


a = Solution()
print(a.largestNumber([3, 30, 34, 5, 9, 352]))
print(a.largestNumber([824, 938, 1399, 5607, 6973, 5703, 9609, 4398, 8247]))
